package com.zzptc.syl.traffic;

import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class Weather1Fragment extends Fragment {

    private int i=0;
    List<Entry> list=new ArrayList<>();
    List<Entry> list2=new ArrayList<>();
    private LineChart line;
    private int k=0;
    private LifeIndex data;
    private final Retrofit retrofit=new Retrofit.Builder()
            .baseUrl("http://hh1.me:5001/")
            .addConverterFactory(GsonConverterFactory.create())
            .build();
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater,
                             @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_weather1, container, false);
        line = (LineChart) view.findViewById(R.id.weather1_lc_wendu);
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                sport();
            }
        },0,3000);
        return view;
    }
    private void sport() {
        Service service=retrofit.create(Service.class);
        Call<LifeIndex> life=service.life();
        life.enqueue(new Callback<LifeIndex>() {
            @Override
            public void onResponse(Call<LifeIndex> call, Response<LifeIndex> response) {
                data=response.body();
                getxAxis(data.get温度());
                i++;
            }
            @Override
            public void onFailure(Call<LifeIndex> call, Throwable t) {

            }
        });
    }
    private void getxAxis(int max) {
        list.add(new Entry(i*3,max));
        LineDataSet lineDataSet=new LineDataSet(list,"        ");
        lineDataSet.setColor(Color.RED);  //折线的颜色
        lineDataSet.setLineWidth(2);        //折线的粗细
        lineDataSet.setDrawCircleHole(false);//实心圆
        //添加第二组数据
        LineData lineData=new LineData(lineDataSet);
        line.setData(lineData);
        //简单美化
        //   X轴所在位置   默认为上面
        line.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM);
        line.getDescription().setEnabled(false);
        line.getXAxis().setDrawGridLines(false);

        //隐藏右边的Y轴
        line.getAxisRight().setEnabled(false);
        line.invalidate();
        XAxis xAxis=line.getXAxis();
        xAxis.setAxisMaximum(20);
//        xAxis.setValueFormatter(new IAxisValueFormatter() {   //X轴自定义坐标
//            @Override
//            public String getFormattedValue(float l, AxisBase axisBase) {
//                for (int i=1;i<=7;i++){
//                    if (l==i){
//                        return "星期"+i;
//                    }
//                }return "";//注意这里需要改成 ""
//            }
//        });
        xAxis.setAxisMaximum(60);   //X轴最大数值
        xAxis.setAxisMinimum(0);   //X轴最小数值
        line.invalidate();
    }
}
